/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameUser;

import Database.CheckConnection;
import Model.Surat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author User
 */
public class UserCariSurat extends JFrame {

    private JLabel awal, akhir;
    private JTextField textawal, textakhir;
    private JButton buttonOk, buttonCancel;
    private ArrayList<Surat> dataSurat;

    public UserCariSurat() {
        this.setTitle("Cari Surat");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(400, 300, 340, 200);
        this.setLayout(null);

        awal = new JLabel("Tanggal Awal");
        awal.setBounds(20, 25, 80, 25);
        this.add(awal);

        akhir = new JLabel("Tanggal Akhir");
        akhir.setBounds(20, 65, 80, 25);
        this.add(akhir);


        textawal = new JTextField();
        textawal.setBounds(110, 25, 190, 25);
        this.add(textawal);

        textakhir = new JTextField();
        textakhir.setBounds(110, 65, 190, 25);
        this.add(textakhir);

        buttonOk = new JButton("OK");
        buttonOk.setBounds(70, 110, 80, 30);
        this.add(buttonOk);
        buttonOk.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (textawal.getText().equals("") || textakhir.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Lengkapi semua data terlebih dahulu");
                } else {
                    dataSurat = new ArrayList<Surat>();
                    Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                    Statement stat = null;
                    try {
                        stat = con.createStatement();
                    } catch (SQLException ex) {
                        Logger.getLogger(UserCariSurat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    String awal = textawal.getText();
                    String akhir = textakhir.getText();
                    String sql = "select * from SURAT where ID_PENERIMA in (select DATA from CACHE) and (TANGGAL_TERIMA between '"+awal+"' and '"+akhir+"')";
                    ResultSet hasil = null;
                    try {
                        hasil = stat.executeQuery(sql);
                    } catch (SQLException ex) {
                        Logger.getLogger(UserCariSurat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        while (hasil.next()) {
                            Surat surat = new Surat();
                            try {
                                surat.setNomor_surat(hasil.getString("NO_SURAT"));
                                surat.setPerihal(hasil.getString("PERIHAL"));
                                surat.setAsal_surat(hasil.getString("ASAL_SURAT"));
                                surat.setPenerima_surat(hasil.getString("PENERIMA_SURAT"));
                                surat.setId_penerima(hasil.getString("ID_PENERIMA"));
                                surat.setLetak_surat(hasil.getString("LETAK_SURAT"));
                                surat.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                                surat.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                                surat.setTerima(hasil.getString("DAFTAR_TERIMA"));
                                dataSurat.add(surat);
                            } catch (Exception ex) {
                                Logger.getLogger(UserCariSurat.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(UserCariSurat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    textawal.setText(null);
                    textakhir.setText(null);
                    TampilCariSurat tampilsurat = new TampilCariSurat(dataSurat);
                    tampilsurat.setVisible(true);
                }
            }
        });

        buttonCancel = new JButton("CANCEL");
        buttonCancel.setBounds(180, 110, 80, 30);
        this.add(buttonCancel);
        buttonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
}
